package com.xbongbong.paas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/12/24 17:12
 * @desrcption: 操作方式
 */
@Getter
public enum RuleOperationEnum {

    /**
     * 操作方式
     */
    NONE(0, "enum.operation.memo.none", "enum.operation.tip.none"),
    ONLY_UPDATE(1, "enum.operation.memo.only_update", "enum.operation.tip.only_update"),
    ONLY_INSERT(2, "enum.operation.memo.only_insert", "enum.operation.tip.only_insert"),
    UPDATE_AND_INSERT(3, "enum.operation.memo.update_and_insert", "enum.operation.tip.update_and_insert"),
    ONLY_DELETE(4, "enum.operation.memo.only_delete", "enum.operation.tip.only_delete");


    RuleOperationEnum(Integer type, String memo, String tip){
        this.type = type;
        this.memo = memo;
        this.tip = tip;
    }

    private Integer type;

    private String memo;
    /**
     * 用于显示在执行日志中
     */
    private String tip;

    public static RuleOperationEnum getByCode(Integer operationType) {
        for (RuleOperationEnum operationEnum: values()) {
            if (Objects.equals(operationEnum.getType(), operationType)) {
                return operationEnum;
            }
        }
        return null;
    }

    public static Map<Integer, RuleOperationEnum> getOperationMap(){
        Map<Integer, RuleOperationEnum> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (RuleOperationEnum operationEnum: values()) {
            map.put(operationEnum.getType(), operationEnum);
        }
        return map;
    }

    public String getTip() {
        return I18nMessageUtil.getMessage(tip);
    }
}
